package com.example.monitordemo.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import com.example.monitordemo.constant.TomcatConstant;
import lombok.experimental.UtilityClass;

import java.util.Date;

import static com.example.monitordemo.config.MonitorConfig.*;
import static com.example.monitordemo.constant.CommonConstant.API_PREFIX;
import static com.example.monitordemo.constant.CommonConstant.INSTANT_QUERY_PREFIX;
import static com.example.monitordemo.constant.TomcatConstant.*;

@UtilityClass
public class TomcatMonitorUtil {

    /**
     * 查询语句
     */
    String promQL;


    /**
     * 服务信息
     * @return
     */
    public JSON serverInfo(Date time, String job, String instance){
        return getJson(time,job,instance,SERVER_INFO);
    }

    /**
     * 最大线程数
     * @return
     */
    public JSON maxThreads(Date time, String job, String instance){
        return getJson(time,job,instance,MAX_THREADS);
    }

    /**
     * 最小空闲线程数
     * @return
     */
    public JSON minSpareThreads(Date time, String job, String instance){
        return getJson(time,job,instance,MIN_SPARE_THREADS);
    }

    /**
     * 接受器线程数
     * @return
     */
    public JSON acceptorThreadCount(Date time, String job, String instance){
        return getJson(time,job,instance,ACCEPTOR_THREAD_COUNT);
    }

    /**
     * 流量接收
     * @return
     */
    public JSON trafficRecv(Date time, String job, String instance){
        return getJson(time,job,instance,TRAFFIC_RECV);
    }

    /**
     * 流量发送
     * @return
     */
    public JSON trafficSend(Date time, String job, String instance){
        return getJson(time,job,instance,TRAFFIC_SEND);
    }

    /**
     * Inspect: 请求数
     * @return
     */
    public JSON requestCount(Date time, String job, String instance){
        return getJson(time,job,instance,REQUEST_COUNT);
    }

    /**
     * Inspect: 错误数
     * @return
     */
    public JSON errorCount(Date time, String job, String instance){
        return getJson(time,job,instance,ERROR_COUNT);
    }

    /**
     * 现有线程数 threads
     * @return
     */
    public JSON currentThreadCount(Date time, String job, String instance){
        return getJson(time,job,instance,CURRENT_THREAD_COUNT);
    }

    /**
     * 现有忙线程数 threads
     * @return
     */
    public JSON currentThreadBusy(Date time, String job, String instance){
        return getJson(time,job,instance,CURRENT_THREAD_BUSY);
    }

    /**
     * 线程连接数 threads
     * @return
     */
    public JSON threadConnectionCount(Date time, String job, String instance){
        return getJson(time,job,instance,THREAD_CONNECTION_COUNT);
    }

    /**
     * 保持连接数 threads
     * @return
     */
    public JSON keepAliveCount(Date time, String job, String instance){
        return getJson(time,job,instance,KEEP_ALIVE_COUNT);
    }

    /**
     * tomcat总会话数 Session count
     * @return
     */
    public JSON tomcatSessionTotal(Date time, String job, String instance){
        return getJson(time,job,instance,TOMCAT_SESSION_TOTAL);
    }

    /**
     * tomcat拒绝会话数 Session count
     * @return
     */
    public JSON tomcatRejectedSessionsTotal(Date time, String job, String instance){
        return getJson(time,job,instance,TOMCAT_REJECTED_SESSIONS_TOTAL);
    }

    /**
     * tomcat 现有会话数
     * @return
     */
    public JSON tomcatCurrentSessionCount(Date time, String job, String instance){
        return getJson(time,job,instance,TOMCAT_CURRENT_SESSION_COUNT);
    }

    /**
     * 前五请求
     * @return
     */
    public JSON top5Request(Date time, String job, String instance){
        return getJson(time,job,instance,TOP5_REQUEST);
    }

    /**
     * 前五错误
     * @return
     */
    public JSON top5RequestError(Date time, String job, String instance){
        return getJson(time,job,instance,TOP5_REQUEST_ERROR);
    }

    /**
     * 接受线程数
     * @return
     */
    public JSON acceptThreadCount(Date time, String job, String instance){
        return getJson(time,job,instance,ACCEPT_THREAD_COUNT);
    }

    /**
     * 轮询器线程数
     * @return
     */
    public JSON pollerThreadCount(Date time, String job, String instance){
        return getJson(time,job,instance,POLLER_THREAD_COUNT);
    }

    /**
     * tomcat过期会话数
     * @return
     */
    public JSON tomcatExpiredSessionsTotal(Date time, String job, String instance){
        return getJson(time,job,instance,TOMCAT_EXPIRED_SESSIONS_TOTAL);
    }

    /**
     * 前五平均处理时间。
     */
    public JSON top5ProcessTime(Date time, String job, String instance){
        return getJson(time,job,instance,TOP5_PROCESS_TIME);
    }

    /**
     * 判断是否替换job和instance
     * @param time 时间
     * @param instance tomcat IP和端口
     * @param job prometheus 配置的tomcat job名
     * @param query 原promQL语句
     * @return
     */
    private static JSON getJson(Date time, String job, String instance, String query) {
        String t = PromQLUtil.parseDateToTime(time);
        promQL = query;
        if (StrUtil.isNotBlank(instance)){
            promQL = promQL.replace(TOMCAT_INSTANCE,instance);
        }
        if (StrUtil.isNotBlank(job)){
            promQL = promQL.replace(TOMCAT_JOB,job);
        }
        String executePromQl = API_PREFIX + INSTANT_QUERY_PREFIX + promQL + "&time="+t;
        return PromQLUtil.executePromQl(executePromQl);
    }

}
